package com.imodia.checkoo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.hsq.commons.util.AuthConfig;
import com.hsq.commons.util.EmailHelper;
import com.hsq.commons.util.StringUtil;
import com.hsq.commons.util.XmlUtil;
import com.hsq.commons.util.db.PageBean;
import com.hsq.commons.util.db.PageResult;
import com.hsq.commons.util.db.SqlParam;
import com.imodia.checkoo.bean.CmsDataDictionaryBean;
import com.imodia.checkoo.bean.CmsJoinJobBean;
import com.imodia.checkoo.bean.CmsOfferJobBean;
import com.imodia.checkoo.bean.CmsUserBean;
import com.imodia.checkoo.service.CmsDataDictionaryService;
import com.imodia.checkoo.service.CmsJoinJobService;
import com.imodia.checkoo.service.CmsOfferJobService;

@Controller
@RequestMapping("/hfmanager/join_job/list.do")
public class CmsJoinJobController {

		private final Log log = LogFactory.getLog(CmsJoinJobController.class);
		
		private final String baseUrl = "join_job";
		private final String url_list = "/hfmanager/" + baseUrl + "/"+baseUrl+"_list";
		private final String url_redirect = "/hfmanager/" +baseUrl + "/list.do?action=list";
		private final String url_edit_redirect = "/hfmanager/" +baseUrl + "/list.do?action=edit";
		private final String url_edit = "/hfmanager/" + baseUrl + "/"+baseUrl+"_edit";
		private final String url_list4job = "/hfmanager/" + baseUrl + "/"+baseUrl+"_list_for_offer";
		

		@Autowired
		private CmsJoinJobService cmsJoinJobService;
		@Autowired
		private CmsOfferJobService cmsOfferJobService;
		@Autowired
		private CmsDataDictionaryService cmsDataDictionaryService;
		@Autowired
		private EmailHelper emailHelper;
		

		
		@RequestMapping(params = "action=delete")
		public ModelAndView delete(HttpServletRequest request) {
			int id = StringUtil.requestInt(request,"id");
			cmsJoinJobService.delete(id);
			return new ModelAndView(new RedirectView(request.getContextPath()+url_redirect), null);
		}

		@RequestMapping(params = "action=save")
		public ModelAndView edit(@ModelAttribute("userMessage")CmsJoinJobBean obj, HttpServletRequest request) {
			int x = 0;
			if(obj.getId()!=null && obj.getId()>0){
				x = cmsJoinJobService.update(obj);
			}else{
				x = cmsJoinJobService.insert(obj);
			}
			return new ModelAndView(new RedirectView(request.getContextPath()+url_edit_redirect+"&id="+obj.getId()+"&x="+x), null);
		}
		
		
		@RequestMapping(params = "action=updateStatus")
		public ModelAndView updateStatus(HttpServletRequest request,CmsJoinJobBean obj) {
			Map<String,Object> m = new HashMap<String,Object>();
			int id = StringUtil.requestInt(request,"id");
			int status = StringUtil.requestInt(request,"status");
			int x = cmsJoinJobService.updateStatus(id,status);
			if(x>0){
				m.put("message","1");
			}else{
				m.put("message","0");
			}
		    return new ModelAndView("/util/public/message", m);
		}
		
		@RequestMapping(params = "action=edit")
		public ModelAndView edit_in(HttpServletRequest request,CmsJoinJobBean obj) {
			int id = StringUtil.requestInt(request,"id");
			Map<String,Object> m = new HashMap<String,Object>();
			CmsJoinJobBean info = cmsJoinJobService.get(id);
			m.put("info", info);
			if(info!=null&& info.getJobId()!=null && info.getJobId()>0){
				CmsOfferJobBean job = cmsOfferJobService.get(info.getJobId());
				m.put("job", job);
				CmsDataDictionaryBean area = cmsDataDictionaryService.get(info.getAreaId());
				m.put("area", area);
			}
			cmsJoinJobService.updateStatus2(id,5);
		    return new ModelAndView(url_edit, m);
		}
		
		@RequestMapping(params = "action=list")
		public ModelAndView list(HttpServletRequest request, CmsJoinJobBean obj, PageBean page) {
			HashMap<String, Object> model = new HashMap<String, Object>();
			SqlParam sql = new SqlParam();
			CmsUserBean user = (CmsUserBean)request.getSession().getAttribute(AuthConfig.SESSION_USER_INFO);
			if(user!=null && user.getId()>1){
				if(request.getSession().getAttribute(AuthConfig.SESSION_AREA_LIST)!=null){
					List<CmsDataDictionaryBean>  areaList = (List<CmsDataDictionaryBean>)request.getSession().getAttribute(AuthConfig.SESSION_AREA_LIST);
					List<String> areaString = new ArrayList<String>();
					for(CmsDataDictionaryBean area:areaList){
						areaString.add(String.valueOf(area.getId()));
					}
					if(areaString.size()>0){
						sql.andIn("area", areaString);
					}else{
						sql.andNotEqualTo(" 1 ", "1");
					}
				}
			}
			int jobId = StringUtil.requestInt(request, "jobId");
			if(jobId>0){
				
				CmsOfferJobBean job = cmsOfferJobService.get(jobId);
				model.put("job", job);
			}
			
			PageResult<CmsJoinJobBean> list = cmsJoinJobService.queryForPageBySql(page,obj);
			model.put("page", list);
			
			List<CmsDataDictionaryBean> areaList = cmsDataDictionaryService.getClildInfo(9);
			model.put("areaList", areaList);
			
			return new ModelAndView(url_list, model);
		}
		
		@RequestMapping(params = "action=list4Job")
		public ModelAndView list4Job(HttpServletRequest request, CmsJoinJobBean obj, PageBean page) {
			HashMap<String, Object> model = new HashMap<String, Object>();
			
			int jobId = StringUtil.requestInt(request, "jobId");
			if(jobId>0){
				CmsOfferJobBean job = cmsOfferJobService.get(jobId);
				model.put("job", job);
			}
			
			PageResult<CmsJoinJobBean> list = cmsJoinJobService.queryForPageBySql(page,obj);
			model.put("page", list);

			List<CmsDataDictionaryBean> areaList = cmsDataDictionaryService.getClildInfo(9);
			model.put("areaList", areaList);
			
			return new ModelAndView(url_list4job, model);
		}
		
		@RequestMapping(params = "action=sendMail")
		public ModelAndView sendMail(HttpServletRequest request, CmsJoinJobBean obj) {
			
			Map<String,Object> m = new HashMap<String,Object>();
			int id = StringUtil.requestInt(request,"id");
			int status = StringUtil.requestInt(request,"status");
			String email = request.getParameter("email");
			CmsJoinJobBean info = cmsJoinJobService.get(id);
			
			
			emailHelper.sendEmail(email, "imya", "dd");
			
			int x = cmsJoinJobService.updateStatus(id,status);
			if(x>0){
				m.put("message","1");
			}else{
				m.put("message","0");
			}
		    return new ModelAndView("/util/public/message", m);
		}
		
		
		private String getEmailContent(CmsJoinJobBean info) {
			String pathString = this.getClass().getResource("").getPath();
			pathString = pathString.substring(0,pathString.indexOf("classes")+7);
			String sql = XmlUtil.getElementValue(pathString+"/email_content.xml", "count_1");
		
			//sql = sql.replaceAll("\\{area\\}", locattionName);
			//sql = sql.replaceAll("\\{other\\}", mailContent);
			return sql;
		}
		
}
